/*
  学习目标：组件通信-子传父
*/
import React from 'react';

export default class Parent extends React.Component {
  state = {
    money: 1000,
  };

  handleMakeMoney = () => {
    this.setState({
      money: 1000 + this.state.money,
    });
  };

  // 1. 在父组件内 定义一个回调函数
  handleCost = (num) => {
    this.setState({ money: this.state.money - num });
  };

  render() {
    return (
      <div>
        <button onClick={this.handleMakeMoney}>爸爸开始赚钱了</button>
        <Child
          money={this.state.money}
          // 2. 通过props传递回调函数
          handleCost={this.handleCost}
        ></Child>
      </div>
    );
  }
}

class Child extends React.Component {
  render() {
    const { handleCost } = this.props;

    return (
      <div>
        <h1>爸爸给我钱了：{this.props.money} </h1>

        <button
          // 3. 子组件内,调用函数
          onClick={() => {
            handleCost(3000);
          }}
        >
          吞金兽要花钱
        </button>
      </div>
    );
  }
}
